<template>
  <div class="app-container">
    <!-- 工具条 -->
    <div>
      <el-button type="danger" plain @click="removeRows()">批量删除</el-button>
    </div>
    <br />
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="searchObj.hosname" placeholder="医院名称" />
      </el-form-item>

      <el-form-item>
        <el-input v-model="searchObj.hoscode" placeholder="医院编号" />
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="fetchData()"
        >查询</el-button
      >
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
      ><el-table-column type="selection" width="55" />
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{ (current - 1) * size + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="hosname" label="医院名称" width="180" />
      <el-table-column prop="hoscode" label="医院编号" width="160" />
      <el-table-column prop="apiUrl" label="地址" width="200" />
      <el-table-column prop="contactsName" label="联系人" />
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          {{ scope.row.status === 1 ? "可用" : "不可用" }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <router-link :to="'/hospset/edit/' + scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit"
              >修改</el-button
            >
          </router-link>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="removeDataById(scope.row.id)"
            >删除</el-button
          >
          <el-button
            v-if="scope.row.status == 1"
            type="primary"
            size="mini"
            icon="el-icon-delete"
            @click="lockHostSet(scope.row.id, 0)"
            >锁定</el-button
          >
          <el-button
            v-if="scope.row.status == 0"
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="lockHostSet(scope.row.id, 1)"
            >取消锁定</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="current"
      :page-size="size"
      :total="total"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />
  </div>
</template>
<script>
import hospsetApi from "@/api/yygh/hospset";

export default {
  // 定义变量和初始值
  data() {
    return {
      listLoading: true, // 是否显示loading信息
      list: null, // 数据列表
      total: 0, // 总记录数
      current: 1, // 页码
      size: 2, // 每页记录数
      searchObj: {}, // 查询条件
      multipleSelection: [], // 批量选择中选择的记录列表
    };
  },
  // 模板渲染前，请求数据
  created() {
    this.fetchData();
  },
  // 定义方法
  methods: {
    fetchData(current = 1) {
      // 当分页组件中的页码发生变化时，会回调该方法，传递变化后的页码
      // current 设置默认值为 1 是页面第一次请求数据时，防止没有传递参数导致报错
      // 将新的页码更新到数据模型中
      this.current = current;
      // 显示 loading 组件直到数据请求完成
      this.listLoading = true;
      hospsetApi
        .getPageList(this.current, this.size, this.searchObj)
        .then((response) => {
          this.list = response.data.rows;
          this.total = response.data.total;
          // 取消 loading 组件的显示
          this.listLoading = false;
        });
    },
    resetData() {
      // 清空查询参数
      this.searchObj = {};
      // 跳转到首页
      this.fetchData();
    },
    // 删除医院设置
    removeDataById(id) {
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 点击确定执行 then
          // 调用删除接口
          hospsetApi.removeById(id).then((response) => {
            if (response.success) {
              // 删除成功后刷新页面
              this.fetchData();
              // 显示成功提示
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          // 点击取消执行 catch
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 锁定和解锁
    lockHostSet(id, status) {
      hospsetApi.lockHospSet(id, status).then((response) => {
        // 刷新页面
        this.fetchData();
      });
    },
    // 复选框选中状态发生变化，回调函数
    handleSelectionChange(val) {
      // val 是当前选中的所有行对象的数组
      // 封装到数据模型中，供批量删除按钮使用
      this.multipleSelection = val;
    },
    // 批量删除
    removeRows() {
      this.$confirm("此操作将永久删除该医院设置信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 提取id列表
          let idList = [];
          for (let i = 0; i < this.multipleSelection.length; i++) {
            idList.push(this.multipleSelection[i].id);
          }
          // 调批量删除接口
          hospsetApi.removeRows(idList).then((response) => {
            // 显示成功提示
            this.$message({
              type: "success",
              message: "删除成功",
            });
            // 刷新页面
            this.fetchData();
          });
        })
        .catch(() => {
          // 点击取消执行 catch
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>